#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

int main(int argc, char const *argv[])
{
     mkfifo("WangWen's message",0666);

    int fd1 = open("ZhangSan's message",O_RDONLY);
    int fd2 = open("WangWen's message",O_WRONLY);

    char buff[24] = "";
    int i = 0;
    for (; i < 2; i++)
    {
        pid_t pid = fork();
        if (pid == 0)
        {
            break;
        }        
    }
    
    if (i == 0)
    {
        while (1)
        {
            char buff[24] = "";
            scanf("%s",buff);
            //fgets(buff,24,0);
            write(fd2,buff,strlen(buff));         
        }
    }
    else if (i == 1)
    {
        while (1)
        {        
            char buff[24] = "";
            read(fd1,buff,24);
            if (strlen(buff) > 0)
            {
                printf("ZhangSan读取到的是%s\n",buff);
            }  
        }
    }    
    else if (i == 2)
    {
        while (1)
        {
            wait(NULL);
        }        
    }
    return 0;
}
